<?php

class Action
{
    public function index() {

        $table_column_title = $table_column = $basis = $data = $from = $to = $search_filter = null;

        extract(URLTracker::init(array(
            'search_filter' => array(
                'goods_id' => array(),
                'category_id' => array(),
                'province_id' => array(),
                'agency_id' => array(),
                'start_date' => $from,
                'end_date' => $to,
                'basis_type' => 0,
                'filter_type' => 0,
                'perspective_agency' => 0,
            ),
        )));

        $GOODS_LIST = goods_config::goods_list();
        $CATEGORY_LIST = goods_config::goods_category();
        $AGENCY_LIST = array_to_filelds_assoc('id', user_helper::get_all_dealers_info());
        $PROVINCE_INDEX = area_helper::province_index();

        $tree = goods_helper::get_category_goods_tree();
        $CATEGORY_GOODS_TREE = array();
        foreach ($tree as $cat_id => $cat) {
            $CATEGORY_GOODS_TREE[$cat_id] = array(
                'title' => $CATEGORY_LIST[$cat_id]['title'],
                'id' => $cat_id,
                'goods_list' => array(),
            );
            foreach ($cat as $goods_id) {
                $CATEGORY_GOODS_TREE[$cat_id]['goods_list'][$goods_id] = array(
                    'title' => $GOODS_LIST[$goods_id]['title'],
                    'id' => $goods_id,
                );
            }
        }

        // 支付时间
        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $from = $search_filter['start_date'];
        }
        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $to = $search_filter['end_date'];
        }

        if (!$from || !$to) {
            $from = $to = date('Y-m-d', strtotime('-1 day'));
            URLTracker::parse(array(
                'search_filter' => array(
                    'start_date' => $from,
                    'end_date' => $to
                )
            ), 1);
        }

        if ($search_filter['basis_type'] == 0 && !empty($search_filter['category_id'])) {
            $basis = $search_filter['category_id'];
            $basis_name = 'category_id';
            $basis_index_list = $CATEGORY_LIST;
        } elseif ($search_filter['basis_type'] == 1 && !empty($search_filter['goods_id'])) {
            $basis = $search_filter['goods_id'];
            $basis_name = 'goods_id';
            $basis_index_list = $GOODS_LIST;
        }

        if ($basis) {

            $filter_radius_name = $filter_radius = null;

            $table_column_title = array(
                '品类/卖品',
                array('出货量', 1, 'int'),
                array('销售额', 1, 'float'),
            );
            $table_column = array(
                'basis_name',
                'sum_num',
                'sum_price',
            );

            $SELECT = array(
                'm.`' . $basis_name . '` basis_name',
                'SUM(m.`real_price`) sum_price',
                'SUM(m.`goods_num`) sum_num',
            );

            $WHERE = ' WHERE 1' .
                     ($from ? ' AND m.`date` >= \'' . $from . '\'' : '') .
                     ($to ? ' AND m.`date` <= \'' . $to . '\'' : '');

            $WHERE .= ' AND m.`' . $basis_name . '` IN (' . join(',', $basis) . ')';
            $FILTER = ' GROUP BY m.`' . $basis_name . '`';

            if ($search_filter['filter_type'] == 0 && !empty($search_filter['province_id'])) {
                $filter_radius = $search_filter['province_id'];
                $filter_radius_name = 'province_code';
            } elseif ($search_filter['filter_type'] == 1 && !empty($search_filter['agency_id'])) {
                $filter_radius = $search_filter['agency_id'];
                $filter_radius_name = 'agency_id';
            }

            if ($filter_radius) {
                $WHERE .= ' AND m.`' . $filter_radius_name . '` IN (' . join(',', $filter_radius) . ')';

                // 只有一个品类或者卖品
                if (count($basis) == 1) {
                    // 如果透视合伙人
                    if ($search_filter['perspective_agency'] == 1) {
                        $SELECT[] = 'm.`province_code`';
                        $SELECT[] = 'm.`agency_id`';
                        $FILTER .= ', m.`agency_id`';
                        $FILTER .= ' ORDER BY m.`province_code` ASC';
                        array_splice($table_column_title, 1, 0, array('地区', '合伙人'));
                        array_splice($table_column, 1, 0, array('province_code', 'agency_id'));
                    } else {
                        $SELECT[] = 'm.`' . $filter_radius_name . '`';
                        $FILTER .= ', m.`' . $filter_radius_name . '`';
                        $FILTER .= ' ORDER BY m.`' . $filter_radius_name . '` ASC';
                        array_splice($table_column_title, 1, 0, '地区/合伙人');
                        array_splice($table_column, 1, 0, $filter_radius_name);
                    }
                } else {
                    URLTracker::del('search_filter', 'perspective_agency');
                }
            }

            // 地区出货,销售额汇总
            $SQL = 'SELECT ' .
                   join(',', $SELECT) .
                   ' FROM `metadata_dealer_order_goods_sku_daily` m' .
                   $WHERE .
                   $FILTER;

            $sums = run_sql('metadata_dealer_order_goods_sku_daily', 'getAll', $SQL);

            $data = array();
            foreach ($sums as $item) {
                $item['basis_name'] = $basis_index_list[$item['basis_name']]['title'];

                if (array_key_exists('province_code', $item) && isset($PROVINCE_INDEX[$item['province_code']])) {
                    $item['province_code'] = $PROVINCE_INDEX[$item['province_code']]['area'];
                }
                if (array_key_exists('agency_id', $item) && isset($AGENCY_LIST[$item['agency_id']])) {
                    $item['agency_id'] = $AGENCY_LIST[$item['agency_id']]['realname'];
                }

                $data[] = $item;
            }
        }

        Response::assign('data', $data);
        Response::assign('table_column', $table_column);
        Response::assign('table_column_title', $table_column_title);
        Response::assign('AGENCY_LIST', $AGENCY_LIST);
        Response::assign('CATEGORY_LIST', $CATEGORY_LIST);
        Response::assign('GOODS_LIST', $GOODS_LIST);
        Response::assign('CATEGORY_GOODS_TREE', $CATEGORY_GOODS_TREE);
        Response::assign('PROVINCE_INDEX', $PROVINCE_INDEX);
        Response::display('admin/goods.html');
    }
}

?>